<?php

namespace App\Admin\Controllers;

use App\Admin\Action\MachineOrder;
use App\Admin\GoodsManage;
use App\Models\MachineBanner;
use App\Models\Machines;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Commoditytype;


class MachinesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '设备管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Machines);

        $grid->column('id', __('Id'));
        $grid->column('name', __('设备名称'));
        $grid->column('address', __('设备地址'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));
        $grid->column('status', __('状态'))->display(function ($status) {
            return config('conf.machines_status')[$status];
        });

        $grid->actions(function ($actions) {
            $actions->add(new GoodsManage);
            $actions->add(new MachineOrder);
            // 去掉查看
            $actions->disableView();
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Machines::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        // $show->field('computing', __('Computing'));
        // $show->field('power_consumption', __('Power consumption'));
        //$show->field('power_ratio', __('Power ratio'));
        $show->field('price', __('Price'));
        $show->field('stock', __('stock'));
        $show->field('original', __('original'));

        $show->field('sold', __('sold'));


        //$show->field('electricity_fee', __('Electricity fee'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('status', __('Status'));
        //$show->field('computing_stock', __('Computing stock'));
        // $show->field('sell_computing', __('Sell computing'));
        //   $show->field('income', __('Income'));
        $show->field('sort', __('Sort'));
        $show->field('type', '商品类型id');
        $show->field('rule', '商品描述');
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Machines);

        $form->text('name', __('设备名称'));
        $form->text('address','设备地址');
        $data=MachineBanner::where('status',1)->get()->toArray();
        foreach ($data as $item){
            $_item=$item["id"];
            $_item1=$item["title"];
            $arr[$_item]=$_item1;
        }
        $form->select('banner_id', __('设备图片'))->options($arr);
        $form->select('status', __('状态'))->options(config('conf.machines_status'));
        // $form->number('sort', __('排序'));
        $form->saving(function (Form $form) {
            $url='http://api.map.baidu.com/geocoder/v2/?address='.$form->address.'&output=json&ak=hjWQQoxgA7mHx00r3XSQycDycbPxpfAi';
            if($result=file_get_contents($url))
            {
                $arr= explode(',"lat":', substr($result, 40,36));
                $form->model()->lat = $arr[0];
                $form->model()->lng = $arr[1];
                $form->model()->save();
            }
        });

        return $form;
    }

}